// pages/person/p/status.js
import Notify from '../../../miniprogram_npm/@vant/weapp/notify/notify'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'

import { querySelfBusinessInfo, signatureFinish, realName } from '../../../api/personApply.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selfBusiness: {},
    progressList: [],
    tipUploadSmsShow: false,
    smsContent: '',
    createDate: '',
    brand: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取下机型
    const res = wx.getSystemInfoSync()
    this.setData({
      brand: res.brand
    })
    this.loadSelfInfo()
  },
  // 加载 个体户 信息
  loadSelfInfo: function(){
    querySelfBusinessInfo().then(res=>{
      res.mobile = res.mobile.substr(0, 3) + '****' + res.mobile.substr(7)
      this.setData({
        selfBusiness: res,
        progressList: JSON.parse( res.progress )
      })
      console.log(this.data)
      this.dateTimeConvert()
    }).catch(res => {
      Notify({ type: 'danger', message: res })
    })
  },
  // 去完成电子签章
  signatureFinishTO: function() {
    wx.navigateTo({
      url: './signature/signature',
    })
  },
  // 完成电子签章
  signatureFinishClick: function() {
    Dialog.confirm({
      message: '是否确认完成电子签章?'
    }).then(() => {
      signatureFinish().then(res => {
        this.setData({
          selfBusiness: res
        })
        this.dateTimeConvert()
      }).catch(err => {
        Notify({ type: 'danger', message: err })
      })
    }).catch(() => {
      // on cancel
    });
  },
  // 去实名采集
  realnameCollectTo: function() {
    wx.navigateTo({
      url: './realname/realname?mobile='+this.data.selfBusiness.mobile+'&taxpayerNumber='+this.data.selfBusiness.taxpayerNumber,
    })
  },
  // 提示上传相关短息
  tipUploadSmsClick: function() {
    this.setData({
      tipUploadSmsShow: true
    })
  },
  getUserInfo(event) {
    const dataParam = {
      'sms': this.data.smsContent
    }
    realName(dataParam).then(res => {
      this.setData({
        selfBusiness: res
      })
      this.dateTimeConvert()
    }).catch(err => {
      Notify({ type: 'danger', message: err })
    })
  },
  onClose() {
    this.setData({
      tipUploadSmsShow: false
    })
  },
  smsContentInput: function(e) {
    const sms = e.detail.value
    console.log(sms)
    this.setData({
      smsContent: sms
    })
  },
  // 查看营业执照
  lockBusinessClick: function(e) {
    wx.navigateTo({
      url: '/pages/license/license',
    })
  },
  // 拷贝网址
  copyPathBtn: function() {
    wx.setClipboardData({
      //准备复制的数据
       data: 'https://etax.zhejiang.chinatax.gov.cn/zjgfdzswj/main/index.html',
       success: function (res) {
         wx.showToast({
           title: '复制成功',
         });
       }
     });
  },
  // 拷贝纳税人识别号
  copyTaxpayerNumberBtn: function() {
    wx.setClipboardData({
      //准备复制的数据
       data: this.data.taxpayerNumber,
       success: function (res) {
         wx.showToast({
           title: '复制成功',
         });
       }
     });
  },
  dateTimeConvert: function(){
    const number = parseInt((this.data.selfBusiness.createDate).toString())
    var date = new Date(number);
    this.setData({
      createDate: date.getFullYear() + "年" + this.formatNumber(date.getMonth()+1) + "月" + this.formatNumber(date.getDate()) + "日"
    })
  },
  formatNumber (n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("进度页面卸载了？")
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("进度页面卸载了？")
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})